﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace OnlineManagement.Common
{
   public static class Conditions
    {
        public static T If<T>(this T t, Predicate<T> predicate, Action<T> action) where T : class
        {
            if (t == null) { throw new ArgumentNullException(); }
            if (predicate(t)) { action(t); }
            return t;
        }
        public static string If(this string s, Predicate<string> predicate, Func<string, string> func)
        {
            return predicate(s) ? func(s) : s;
        }
        public static T Switch<T>(this T t, Predicate<T> predicate, Action<T> action) where T : class
        {
            if (t == null) { throw new ArgumentNullException(); }
            if (predicate(t)) { action(t); }
            return t;
        }

        public static TOutput Switch<TOutput, TInput>(this TInput input, IEnumerable<TInput> inputSource, IEnumerable<TOutput> outputSource, TOutput defaultOutput)
        {
            IEnumerator<TInput> inputIterator = inputSource.GetEnumerator();
            IEnumerator<TOutput> outputIterator = outputSource.GetEnumerator();

            TOutput result = defaultOutput;
            while (inputIterator.MoveNext())
            {
                if (outputIterator.MoveNext())
                {
                    if (input.Equals(inputIterator.Current))
                    {
                        result = outputIterator.Current;
                        break;
                    }
                }
                else break;
            }
            return result;
        }
    }
}
